package com.song.exception.controller;


import com.song.pojo.Emp;
import com.song.pojo.LoginInfo;
import com.song.pojo.Result;
import com.song.service.EmpService;
import com.song.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
//@RequestMapping("/login")
public class LoginController {
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        log.info("登录信息：{}", emp);
        LoginInfo loginInfo = empService.login(emp);
        if(loginInfo != null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginInfo.getId());
            claims.put("username", loginInfo.getUsername());
            String token = JwtUtils.generateToken(claims);
            loginInfo.setToken(token);
            return Result.success(loginInfo);
        }else{
            return Result.error("用户名或密码错误");
        }
    }
}
